<?php

namespace App\Http\Requests\Admin\Question\Tag;

use App\Models\QuestionTag;
use Illuminate\Foundation\Http\FormRequest;

class CreateRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */

    public function rules()
    {
        return [
            'name' => ['required', function ($attribute, $value, $fail) {
                $where = [];
                $where['name'] = $value;
                $tag = QuestionTag::where($where)->count('id');
                if (!$tag) {
                    return true;
                } else {
                    $fail(':attribute已存在.');
                }

            }],
        ];
    }

    public function attributes()
    {
        return [
            'name' => '标签名称'
        ];
    }
}
